<template>
    <div>
        <p><b>您正在下载文档 </b></p>
        <p>后台正在为您打包，请耐心稍等。</p>
        <!-- <p>如果您无法正常下载，<span @click="exportWordFile">请点击此处重试</span></p> -->
    </div>
</template>
<script>
    import { exportWord } from "@/api/project/project";
    export default {
        name:'downloadPage',
        data(){
            return {
                projectId:this.$route.query.projectId
            }
        },
        mounted(){
            this.$nextTick(()=>{
                this.exportWordFile();
            })
        },
        methods:{
            exportWordFile(){
                exportWord({
                    projectId:this.projectId
                }).then(ret => {
                    var title = decodeURI(ret.disposition.split('="')[1].split('"')[0]);
                    var blob = ret;
                    // FileReader主要用于将文件内容读入内存
                    var reader = new FileReader();
                    reader.readAsDataURL(blob);
                    // onload当读取操作成功完成时调用
                    reader.onload = function(e) {
                        var a = document.createElement('a');
                        a.download = title;
                        a.href = e.target.result;
                        document.body.appendChild(a);
                        a.click();
                        document.body.removeChild(a);
                    }
                }).catch();
            }
        }
    }
</script>
<style scoped>
    div{
        font-size:16px;
        text-align:center;
        margin:15% auto;
    }
    b{
        font-size:22px;
    }
    span{
        color:#96bfec;
        text-decoration:underline;
        cursor:pointer;
    }
</style>